<!--<script src="__TMPL__/public/assets/js/jquery-1.10.2.min.js"></script>-->
<!--<script src="__STATIC__/js/wind.js"></script>-->
<!--<script src="__TMPL__/public/assets/js/bootstrap.min.js"></script>-->

<title>游戏页面：关闭页面将关闭游戏</title>
<body>
<h1>关闭游戏，执行游戏关闭操作</h1>
<p>请等待页面提示消息后在关闭页面</p>
<div class="open"></div>
<div class="close"></div>
<div class="err"></div>

</body>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript" src="/static/wxshare/share/js/socket.io.js"></script>
<!--<script src="/socket/socket.io.js"></script>-->
<script>
	var mobileid;
	var socket_url;
	var appapi_url;
	if (window.location.hostname == 's1.scbjxs.com') {
		socket_url = "http://s2.scbjxs.com:19967";
		appapi_url = 'https://s1.scbjxs.com/appapi/index.php';
	} else {
		socket_url = "http://s1.zhiboxiu.com.cn:19967";
		appapi_url = 'https://s1.zhiboxiu.com.cn/appapi/index.php';
	}

	// 获取当前链接
	//   如果是 https 则替换成 http 并转跳
	let currentUrl = $(location).attr('href');
	console.log('前', currentUrl)
	if (currentUrl.indexOf("https") >= 0) {
		currentUrl = currentUrl.replace('https', 'http');
		console.log('后', currentUrl)
		setTimeout(function () {
			window.location.href = currentUrl;
		}, 2000)
	}


	var socket = new io(socket_url);
	// 与服务器连接成功
	socket.on('connect', () => {
		mobileid = socket.id
		console.log('socket链接:' + socket_url)
		console.log('socket链接成功！' + socket.id)
	})

	// 与服务器连接成功
	socket.on('disconnect', () => {
		console.log('socket链接:断开')
		console.log('socket链接:断开成功！')
	})

	// 接受服务器消息
	socket.on('broadcastingListen', function (data) {
		//console.log(data[0]);
		//console.log(JSON.parse(data[0]).msg[0]);
		var listenMsg = JSON.parse(data[0]).msg[0];
		console.log('接收服务器消息', JSON.stringify(listenMsg));

		// 如果是金花游戏
		if (listenMsg._method_ && listenMsg._method_ == 'jinhuaLiveGame') {
			$(".open").append(listenMsg.commit + listenMsg.ct + '<br>');
			// 关闭窗口
			setTimeout(function () {
				window.close("", "_self", "")
				$(window).unload()
			}, 5000)
		}
		if (listenMsg._method_ && listenMsg._method_ == 'jinhuaLiveGameEnd') {
			$(".close").append(listenMsg.commit + listenMsg.ct + '<br>');
			$(".close").append('请稍等，关闭直播间中。。。。。<br>');
			// 关闭直播
			if ('炸金花游戏结束' == listenMsg.commit) {
				delLive()
				// 关闭窗口
				/*setTimeout(function () {
					window.close("", "_self", "")
					$(window).unload()
				}, 5000)*/
			}
		}
		if (listenMsg._method_ && listenMsg._method_ == 'no_room') {
			$(".close").append(listenMsg.commit + listenMsg.ct + '<br>');
			// 关闭直播
			delLive()
			// 关闭窗口
			/*setTimeout(function () {
				window.close("", "_self", "")
				$(window).unload()
			}, 5000)*/
		}
	});

	// 链接的时候 接收服务器的消息
	socket.on('conn', function (data) {
		console.log(data);
		if (data[0] !== 'no') {
			//console.log('接收服务器进入房间的消息', JSON.parse(data).msg[0]);
			var _return = JSON.parse(data);
			var listenMsg = _return.msg[0];
			if (_return.retmsg === 'ERR') {
				$(".err").append(listenMsg.ct + '<br>');
				setTimeout(function () {
					//window.location.href = currentUrl+"&is_force=1";
					delLive()
				}, 1000)
			}else{
				// 准备发送消息
				setTimeout(function () {
					toSend();
				}, 1000)
			}
			console.log('接收服务器进入房间的消息', JSON.stringify(listenMsg));
		}
	});


	/*setTimeout(function () {
		sendSocket("{$uid}", "{$roomnum}", "{$token}");
	},1000)

	setTimeout(function () {
		alert('添加成功!');
	},3000)*/

	// 准备发送消息
	toSend();

	function toSend() {
		if (mobileid) {
			enterRoom("{$uid}", "{$token}", "{$stream}", mobileid)
			setTimeout(function () {
				sendSocketGame("{$uid}", "{$roomnum}", "{$token}");
			}, 1000)
		} else {
			setTimeout(function () {
				toSend();
			}, 1000)
		}
	}

	// 关闭直播
	function delLive(){
		let url = "{:url('Liveing/del',array('uid'=>$uid))}";
		setTimeout(function () {
			$.ajax({
				type: "get",
				url: url,
				data: {},
				dataType: 'json',
				success: function (res) {
					console.log('关闭直播间',res)
					if (res.code == 1) {
						$(".close").append('关闭直播间成功！<br>');
						// 关闭窗口
						setTimeout(function () {
							window.close("", "_self", "")
							$(window).unload()
						}, 1000)
					} else {
						console.log("信息初始化失败，请刷新");
					}
				}
			});
		}, 2000)
	}

	// 进入直播间
	function enterRoom(liveuid, token, stream, mobileid) {
		// 直播间是独立的换成 需要请求后台设置当前登录用户的换成
		$.ajax({
			type: "post",
			url: appapi_url,
			data: {
				service: 'Live.enterRoom',
				uid: liveuid,
				token: token,
				liveuid: liveuid,
				stream: stream,
				city: '',
				mobileid: mobileid
			},
			dataType: 'json',
			success: function (res) {
				if (res.ret == 200) {
					var msgString = {
						uid: liveuid,
						roomnum: liveuid,
						stream: stream,
						//nickname: user_nickname,
						equipment: 'pc',
						isvideo: 1,
						token: token
					}
					socket.emit('conn', msgString);
					console.log('进入直播间', msgString);
				} else {
					console.log("信息初始化失败，请刷新");
				}
			}
		});
	}

	// 发送socket测试:结束游戏
	function sendSocketGame(uid, roomnum, token) {
		var msgString = {
			retcode: "000000",
			retmsg: "ok",
			msg: [
				{
					_method_: "endJinhuaHand",
					uid: uid,
					roomnum: roomnum,
					token: token,
					commit: "开启金花游戏发送消息",
					ct: ""
				}
			]
		}
		//socket.emit('broadcast', JSON.stringify(msgString));
		socket.emit('broadcast', msgString);
		console.log('模拟发送消息', JSON.stringify(msgString));
	}
</script>